<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Sync extends CI_Controller {

    public function __construct() {
        parent::__construct();
		$this->load->model('batch_model');
		$this->load->helper(array('form', 'url'));
    }

    function index() {
        $this->load->helper('download');
		$tanggal=date('Ymd');
		$namaFile='Backup Database Ecamp - '.$tanggal . '.sql.zip';
		$this->load->dbutil();
		$backup=& $this->dbutil->backup();
		force_download($namaFile, $backup);
		redirect ('batch/batch');
    }
	
	public function form_database() {
        $data['title'] = 'Form Database';

        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'form_database', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
	}
	
	public function upload() {
				$field_name = 'file_data';
                $config['upload_path'] = './file_db';
				$config['allowed_types'] = '*';
				
				$this->load->library('upload', $config);
                $files = $this->upload->do_upload($field_name);
				
				if (!$files){
				redirect('sync/form_database');
				}
				
				else {
                    $data = $this->upload->data($field_name); //get file_name
                    $file_name = $data['file_name'];
                    foreach ($file_name as $file)
					 {
					   $file_restore = $this->load->file('file_db/' . $file, true);
					   $file_array = explode(';', $file_restore);
					   foreach ($file_array as $query)
						{
						$this->db->query("SET FOREIGN_KEY_CHECKS = 0");
						$this->db->query($query);
						$this->db->query("SET FOREIGN_KEY_CHECKS = 1");
						}
					  }
					}
				redirect('batch/batch');
	}
	
}

